<?php

class ClientsController extends Controller
{
	public function filters() {
		return array(
			array('application.filters.isAuthorizedFilter'),
			array('application.filters.accessControlFilter + list','roles'=>array(Users::ADMIN_ROLE,Users::MANAGER_ROLE)),
		);
	}

	public function actionSearch()
	{
		$dataProvider = null;

		$SearchForm = new SearchForm();

		if(isset($_GET['SearchForm'])){
			$SearchForm->attributes = Yii::app()->input->stripClean($_GET['SearchForm']);
			if($SearchForm->validate()){
				$dataProvider = $SearchForm->searchClient();
			}
		}

		$this->render('search',array('SearchClientForm'=>$SearchForm,'dataProvider'=>$dataProvider));
	}

	public function actionList()
	{
		$model = new Clients('search');
		$model->unsetAttributes();
		if(isset($_GET['Clients'])){
			$model->attributes=$_GET['Clients'];
		}
		$this->render('list', array(
			'model'=>$model,
		));
	}

	public function actionAdd($surname = null,$name = null,$patronymic = null, $id = null)
	{
		if($id and ($model = Clients::model()->findByPk($id)) !== null){
			$reg_address = $model->reg_address;
			$hab_address = $model->hab_address;
			$current_passport = $model->getCurrentPassport();

			if(($prev_passport = $model->getPrevPassport())===null)
				$prev_passport = new Passports();
		} else {
			$model = new Clients();
			$model->surname = Yii::app()->input->stripClean($surname);
			$model->name = Yii::app()->input->stripClean($name);
			$model->patronymic = Yii::app()->input->stripClean($patronymic);

			$reg_address = new Addresses();
			$hab_address = new Addresses();

			$current_passport = new Passports();
			$prev_passport = new Passports();
		}

		$current_passport->setScenario('currentPassport');

		if(!empty($_POST)){
			$model->attributes = Yii::app()->input->stripClean($_POST['Clients']);
			$model->prev_passport = isset($_POST['prev_passport_data'])?true:false;

			$reg_address->attributes = Yii::app()->input->stripClean($_POST['Addresses'][1]);
			$reg_address->type = Addresses::TYPE_REGISTRATION;
			$reg_address->client_id = 0;

			$current_passport->attributes = Yii::app()->input->stripClean($_POST['Passports'][1]);
			$current_passport->client_id = 0;

			if($model->prev_passport){
				$prev_passport->attributes = Yii::app()->input->stripClean($_POST['Passports'][2]);
				$prev_passport->client_id = 0;
			}

			if(isset($_POST['hab-address-check'])){
				$hab_address->attributes = Yii::app()->input->stripClean($_POST['Addresses'][2]);
			} else {
				$hab_address->attributes = $reg_address->attributes;
			}
			$hab_address->type = Addresses::TYPE_HABITATION;
			$hab_address->client_id = 0;

			$model_errors = $model->validate()?false:true;
			$reg_address_errors = $reg_address->validate()?false:true;
			$hab_address_errors = $hab_address->validate()?false:true;
			$current_passport_errors = $current_passport->validate()?false:true;
			$prev_passport_errors = (($model->prev_passport and $prev_passport->validate()) or !$model->prev_passport)?false:true;

			if(!$model_errors and !$reg_address_errors and !$hab_address_errors and !$current_passport_errors and !$prev_passport_errors){
				$model->save(false);

				$reg_address->client_id = $model->id;
				$reg_address->save(false);

				$hab_address->client_id = $model->id;
				$hab_address->save(false);

				$current_passport->client_id = $model->id;
				$current_passport->type = Passports::TYPE_CURRENT;
				$current_passport->save(false);

				if($model->prev_passport){
					$prev_passport->client_id = $model->id;
					$prev_passport->type = Passports::TYPE_PREV;
					$prev_passport->save(false);
				}

				$this->redirect(Yii::app()->createUrl('clients/view',array('id'=>$model->id)));
			}
		}

		$this->render('add',array(
			'model'=>$model,
			'reg_address'=>$reg_address,
			'hab_address'=>$hab_address,
			'current_passport'=>$current_passport,
			'prev_passport'=>$prev_passport,
		));
	}

	public function actionView($id)
	{
		$model = Clients::model()->findByPk($id);
		if($model === null){
			$this->redirect(Yii::app()->createUrl('clients/search'));
		}

		Yii::app()->searchHistory->addClientEntry($model->id,$model->fio);

		$this->render('view',array('model'=>$model));
	}
}